package com.xhl.oauth2.config.oauth2;

import com.xhl.oauth2.base.R;
import com.xhl.oauth2.base.constant.ResponseConstant;
import com.xhl.oauth2.base.utils.ServletUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("restAccessDeniedHandler")
public class RestAccessDeniedHandler implements AccessDeniedHandler {
    //处理权限不足的情况:403,对应:access_denied
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException)
            throws IOException, ServletException {
        System.out.println("-------RestAccessDeniedHandler");
        ServletUtil.printRestResult(R.error(ResponseConstant.OAUTH2_403.getCode(),ResponseConstant.OAUTH2_403.getMsg()));
    }
}
